Here are the steps we use to do nonlinear regression in R:

1. Create a vector of time data, and a vector of concentration data.

In R, you can develop arrays called vectors from data sets, but in this example, we create each

vector manually, naming them Time and Conc, using the data from Table 19-2:

In the two preceding equations, c is a built-in R function for combine that creates an array (see

Chapter 2) as a vector from the lists of numbers.

2. Specify the equation to be fitted to the data, using the algebraic syntax your software

requires.

We write the equation using R’s algebraic syntax this way: Conc ~ C0 * exp(– ke * Time), where

Conc and Time are your vectors of data, and C0 and ke are parameters you set.

3. Tell the software that C0and keare parameters to be fitted, and provide initial estimates for

these values.

Nonlinear curve-fitting is a complicated task solved by the software through iteration. This means

you give it some rough estimates, and it refines them into closer estimates to the truth, repeating

this process until it arrives at the best-fitting, least-squares solution.

Coming up with starting estimates for nonlinear regression problems can be tricky. It’s more of an

art than a science. If the parameters have physiological meaning, you may be able to make a guess

based on known physiology or past experience. Other times, your estimates have to be trial and

error. To improve your estimates, you can graph your observed data in Microsoft Excel, and then

superimpose a curve from values calculated from the function for various parameter guesses that

you type in. That way, you can play around with the parameters until the curve is at least in the

ballpark of the observed data.

In this example, C0 (variable C0) is the concentration you expect at the moment of dosing (at

).

From Figure 19-6, it looks like the concentration starts out around 50, so you can use 50 as an

initial guess for C0. The ke parameter (variable ke) affects how quickly the concentration

decreases with time. Figure 19-6 indicates that the concentration seems to decrease by half about

every few hours, so λ should be somewhere around 4 hours. Because

, a little

algebra gives the equation ke= 0.693/X. If you plug in 4 hours for X, you get ke = 0.693/4 = 0.2, so

you may try 0.2 as a starting guess for ke. You tell R the starting guesses by using the syntax:

start=list(

,

).

The statement in R for nonlinear regression is nls, which stands for nonlinear least-squares. The full

R statement for executing this nonlinear regression model and summarizing the output is:

Interpreting the output

As complicated as nonlinear curve-fitting may be, the output is actually quite simple. It is formatted

and interpreted like the output from ordinary linear regression. Figure 19-7 shows the relevant part of

R’s output for this example.